package com.example.demo.controller;

import com.example.demo.domain.vo.HistoricIdentityLinkVO;
import com.example.demo.domain.vo.HistoricProcessInstanceVO;
import com.example.demo.domain.vo.HistoryTaskInstanceVO;
import com.example.demo.domain.vo.IdentityLinkVO;
import com.example.demo.domain.vo.ProcessInstanceVO;
import com.example.demo.domain.vo.TaskVO;
import com.example.demo.service.ActivitiService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 *
 * @Description:
 * @Author: zsg
 * @Date: 2018/07/02 15:24
 */
@RestController
@RequestMapping("/activiti")
public class ActivitiController {

    @Autowired
    private ActivitiService activitiService;

    /**
     *  开始流程
     * @param processKey 流程图的key
     * @param userId 执行人的id
     * @return
     */
    @GetMapping("startProcess")
    public List<ProcessInstanceVO> startProcess(@RequestParam("processKey") String processKey, @RequestParam("userId") String userId){
        List<ProcessInstanceVO> processInstanceVOS = activitiService.startProcess(processKey, userId);
        return processInstanceVOS;
    }


    /**
     * 获取个人的对应任务列表
     * @param userId
     * @return
     */
    @GetMapping("getPersonTask")
    public List<TaskVO> getPersonTask(@RequestParam("userId") String userId){
        List<TaskVO> myTaskList = this.activitiService.findMyTaskList(userId);
        return  myTaskList;
    }

    /**
     * 完成任务
     * @param taskId
     * @return
     */
    @GetMapping("completeTask")
    public String completeTask(@RequestParam("taskId") String taskId){
        this.activitiService.competeCurrentTask(taskId);
        return "完成了任务";
    }

    /**
     * 删除组成员
     * @param taskId
     * @param userId
     * @return
     */
    @GetMapping("deleteGroupUser")
    public String deleteGroupUser(@RequestParam("taskId") String taskId,@RequestParam("userId") String userId){
        this.activitiService.deleteGroupUser(taskId,userId);
        return userId+"删除成功";
    }

    /**
     * 添加组任务成员
     * @param taskId
     * @param userId
     * @return
     */
    @GetMapping("addGroupUser")
    public String addGroupUser(@RequestParam("taskId") String taskId,@RequestParam("userId") String userId){
        this.activitiService.addGroupUser(taskId, userId);
        return userId+"添加组任务成功";
    }
    /**
     * 用户查询自己的审批的历史记录
     * @param userId
     * @return
     */
    @GetMapping("findHistoryTask")
    public List<HistoryTaskInstanceVO> findHistoryTask(String userId){
       return this.activitiService.findHistoryTask(userId);
    }

    /**
     *  开始组流程
     * @param processKey 流程图的key
     * @param userIds 执行人的id
     * @return
     */
    @GetMapping("startGroupProcess")
    public ProcessInstanceVO startGroupProcess(@RequestParam("processKey") String processKey, @RequestParam("userIds") String userIds){
        return activitiService.startGroupProcess(processKey,userIds);
    }

    /**
     * 拾取任务
     * @param taskId
     * @param userId
     * @return
     */
    @GetMapping("claimTask")
     public  String claimTask(@RequestParam("taskId") String taskId,@RequestParam("userId") String userId){
        this.activitiService.claim(taskId,userId);
        return  userId+"拾取任务成功";
     }

    /**
     * 查询组任务成员历史列表
     * @param taskId
     * @return
     */
     @GetMapping("getHistoryGroupUser")
     public List<HistoricIdentityLinkVO> getHistoryGroupUser(@RequestParam("taskId") String taskId){
         List<HistoricIdentityLinkVO> groupUser = this.activitiService.findHistoryGroupUser(taskId);
         return  groupUser;
     }

    /**
     * 查询组用户
     * @param taskId
     * @return
     */
     @GetMapping("getGroupUser")
    public List<IdentityLinkVO> getGroupUser(String taskId) {
        List<IdentityLinkVO> groupUser = this.activitiService.findGroupUser(taskId);
        return  groupUser;
    }

    /**
     * 排他网关
     * @param processKey
     * @param money
     * @return
     */
    @GetMapping("startExclusiveGateWayProcess")
    public ProcessInstanceVO startExclusiveGateWayProcess(@RequestParam("processKey") String processKey, @RequestParam("money") double money){
        ProcessInstanceVO processInstanceVO = this.activitiService.startExclusiveGateWayProcess(processKey, money);
        return processInstanceVO;
    }

    /**
     * 并行网关
     * @param processKey
     * @return
     */
    @GetMapping("startParallelGateWayProcess")
    public  List<ProcessInstanceVO> startParallelGateWayProcess(@RequestParam("processKey") String processKey){
        List<ProcessInstanceVO> processInstanceVOList = this.activitiService.startparallelGateWayProcess(processKey);
        return processInstanceVOList;
    }

    /**
     * 结束任务
     * @param processInstanceId
     * @param reason
     * @return
     */
    @GetMapping("deleteProcess")
    public String deleteProcess(@RequestParam("processInstanceId") String  processInstanceId,@RequestParam("reason") String reason){
        this.activitiService.deleteProcess(processInstanceId,reason);
        return processInstanceId+"结束";
    }

    /**
     * 委托任务
     * @param taskId
     * @param userId
     * @return
     */
    @GetMapping("ownerTask")
    public String ownerTask(@RequestParam("taskId") String taskId,@RequestParam("userId") String userId){
        this.activitiService.ownerTask(taskId, userId);
        return taskId+"任务委托给"+userId;
    }

    /**
     * 完成委托任务
     * @param taskId
     * @return
     */
    @GetMapping("completeOwnerTask")
    public String completeOwnerTask(@RequestParam("taskId") String taskId){
        this.activitiService.completeOwnerTask(taskId);
        return taskId+"任务已经完成";
    }

    /**
     * 查询历史流程
     * @param processInstanceId
     * @return
     */
    @GetMapping("getHistoryProcessInstance")
    public HistoricProcessInstanceVO  getHistoryProcessInstance(@RequestParam("processInstanceId") String processInstanceId){
        HistoricProcessInstanceVO historyProcessInstance = this.activitiService.findHistoryProcessInstance(processInstanceId);
        return historyProcessInstance;
    }
    /**
     * 再页面获取对应的流程图
     * @param response
     * @param processInstanceId
     */
    @GetMapping("image")
    public void image(HttpServletResponse response,
                      @RequestParam String processInstanceId) {
        try {
            InputStream is = activitiService.getDiagram(processInstanceId);
            if (is == null){
                return;
            }

            response.setContentType("image/png");

            BufferedImage image = ImageIO.read(is);
            OutputStream out = response.getOutputStream();
            ImageIO.write(image, "png", out);

            is.close();
            out.close();
        } catch (Exception ex) {
            System.out.println("打印图片出错");
        }
    }


}
